package com.atXiao.food.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.dao.UserHistoryDao;
import com.atXiao.food.entity.UserHistoryWithUserId;
import com.atXiao.food.reqbody.history.HistoryBroseReq;
import com.atXiao.food.rspbody.history.UserHistory;
import com.atXiao.food.service.UserHistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lixiaoxiao
 * @date: 2023/11/24 10:46
 * @version: 1.0
 * @desc:
 */
@Service
public class UserHistoryServiceImpl implements UserHistoryService {

    public final static Log logger = new Log(UserHistoryServiceImpl.class);

    @Autowired
    UserHistoryDao userHistoryDao;


    @Override
    public List<UserHistory> queryUserHistory(String trace, HistoryBroseReq requestory) {
        // 用户编号
        String userId = requestory.getUserId();
        // 查询历史浏览记录
        List<UserHistoryWithUserId> historyList = userHistoryDao.getUserHistoryWithUserId(userId);
        List<UserHistory> userHistoryList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(historyList)) {
            userHistoryList = historyList.stream().map(item -> {
                UserHistory history = new UserHistory();
                BeanUtils.copyProperties(item, history);
                return history;
            }).collect(Collectors.toList());
        }
        logger.info(trace, BusSignConstant.TASTE_FOOD, BuscodeConstant.HISTORY_SEARCH,"用户历史记录:"+ JSONObject.toJSONString(userHistoryList));
        return userHistoryList;
    }
}
